package com.snowball.demo.mvc.service.impl;

import com.aliyun.openservices.ons.api.Consumer;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.snowball.demo.mvc.entity.Movie;
import com.snowball.demo.mvc.mapper.MovieMapper;
import com.snowball.demo.mvc.service.MovieService;
import com.snowball.mq.spring.service.MqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author genezhao
 * @since 2018-07-23
 */
@Slf4j
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    private final static String prefix = "movie:";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MqService mqService;

    @Override
    public void addMovie(Movie movie) {
        baseMapper.insert(movie);
        redisTemplate.opsForValue().set(prefix+movie.getMovieNo(), movie);

        //mq example
        Consumer consumer = mqService.createConsumer();
//        consumer.start();
    }

    @Override
    public Movie queryMovieByNo(String no) {
        Movie movie = (Movie) redisTemplate.opsForValue().get(prefix + no);
        if (null != movie) {
            log.debug("从redis缓存获取数据");
//            throw new BusinessException("获取电影信息异常");
            return movie;
        }

        movie = new Movie();
        movie.setMovieNo(no);
        movie = baseMapper.selectOne(movie);
        if (null != movie) {
            redisTemplate.opsForValue().set(prefix + no, movie);
        }
        return movie;
    }


}
